package com.api.gbhg.luoao.web;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.luoao.utils.CollectUtils;
import com.api.gbhg.zengping.page.PageInfo;
import com.api.gbhg.zengping.utils.RecursionUtil;
import com.google.common.base.Joiner;
import weaver.conn.RecordSet;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Path("/Collect")
public class Collect {

    /**
     * 汇总核批上报
     *
     * @return
     */
    @POST
    @Path("/report")
    @Produces(MediaType.APPLICATION_JSON)
    public String getreport(String[] requestId) {
        Map<String, Object> map = CollectUtils.PoolData(requestId);
        return JSON.toJSONString(map);
    }


    /**
     * 汇总上报核批列表
     *
     * @return
     */
    @POST
    @Path("/ReportLsit")
    @Produces(MediaType.APPLICATION_JSON)
    public String getReportLsit(@Context HttpServletRequest request, @Context HttpServletResponse response, Map<String, Object> param) {
        User user = HrmUserVarify.getUser(request, response);  //得到当前用户
        int userId = user.getUID(); //得到当前用户id
        RecordSet collectLsit = CollectUtils.getCollectLsit(param);
        Map<String, Object>[]  maps = new HashMap[collectLsit.getArray().size()];
        int total = 0;
        //判断是否是独立核算单位
        RecordSet getunit = new RecordSet();
        getunit.executeQuery("select * from hrmresource where id = " + userId);
        if (getunit.next()) {
            String lastname = getunit.getString("lastname");
            if (lastname.equals("李升亮")) {

                int i = 0;
                while (collectLsit.next()) {
                    maps[i] = new HashMap<>();
                    maps[i].put("id", collectLsit.getStringNoTrim("id"));
                    maps[i].put("dwmc", collectLsit.getStringNoTrim("subcompanyname"));
                    maps[i].put("lx", CollectUtils.getTypeName(collectLsit.getStringNoTrim("lx")));

                    BigDecimal hzje = new BigDecimal(collectLsit.getStringNoTrim("hzje"));
                    maps[i].put("hzje", hzje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                    maps[i].put("xms", collectLsit.getStringNoTrim("xms"));

                    BigDecimal hzhpje = new BigDecimal(collectLsit.getStringNoTrim("hzhpje"));
                    maps[i].put("hzhpje", hzhpje.divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP));

                    maps[i].put("shzt", collectLsit.getStringNoTrim("shzt"));
                    maps[i].put("spyj", collectLsit.getStringNoTrim("spyj"));
                    maps[i].put("hzxmjh", CollectUtils.getProcessInformation(CollectUtils.getrequestId(collectLsit.getStringNoTrim("id"))));
                    i++;
                }
                total = CollectUtils.getcount();
            }
        }


        PageInfo page = new PageInfo();
        page.setPageNum(Integer.parseInt(param.get("pageNum").toString()));
        page.setPageSize(Integer.parseInt(param.get("pageSize").toString()));
        page.setTotal(total);
        page.setList(maps);
        return JSON.toJSONString(page);
    }


    /**
     * 批准汇总上报项目
     *
     * @return
     */
    @POST
    @Path("/approve")
    @Produces(MediaType.APPLICATION_JSON)
    public String getapprove(Map<String, Object>[] maps) {
//        boolean updatathzsbxm = CollectUtils.getUpdatathzsbxm(param);
//        Map<String, Object> maps = new HashMap<>();
//        maps.put("result", updatathzsbxm);
//        maps.put("requsetId", CollectUtils.getrequestID(param));
//        return JSON.toJSONString(maps);
        List<String> requestId = new ArrayList<>();
        boolean b = false;
        if (maps.length != 0 || maps != null) {
            //更新汇总上报放回科室表
            b = CollectUtils.Updatahzsbfhks(maps);//汇总上报返回科室表
            if (b) {
                for (Map<String, Object> pr : maps) {
                    requestId.add(pr.getOrDefault("requestId", "").toString());
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("requestId", Joiner.on(",").join((Iterable<?>) requestId));

        return JSON.toJSONString(map);

    }

    /**
     * 不批准汇总上报项目
     *
     * @param param
     * @return
     */
    @POST
    @Path("/disagree")
    @Produces(MediaType.APPLICATION_JSON)
    public String getdisagree(Map<String, Object>[] param) {
        boolean b = false;
        if (param.length != 0 || param != null) {
            //更新汇总上报放回科室表
            b = CollectUtils.Updatahzsbfhks(param);//汇总上报返回科室表
            if (b) {
                CollectUtils.InsertProcess(param);//更新流程中的重批金额
                Map<String, Object>[] processnode = CollectUtils.getProcessnode(param);
                return JSON.toJSONString(processnode);
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("result", b);

        return JSON.toJSONString(map);
    }


    /**
     * 单位框
     * @return
     */
    @GET
    @Path("/Unitbox")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUnitbox(){
        //系统管理员,直接返回拱北海关
        List dw = new ArrayList();
        dw.add(0);
        Map[] subMap = new RecursionUtil().getCurrentDept(dw);
        return JSON.toJSONString(subMap);
    }



}
